//
//  SWInvitationViewController.swift
//  ChildStory
//  邀请界面，发送邀请码
//  Created by linhan on 16/8/31.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWInvitationViewController: SWBaseViewController,SWAPIComponent
{
    lazy private var _telTextField:UITextField = UITextField()
    lazy private var _shareView:UIView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    private func setup()
    {
        view.backgroundColor = UIColor.white
        title = "邀请Ta加入小屋"
        
        let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(viewTapped))
        view.addGestureRecognizer(tapGesture)
        
        let TextFieldWidth:CGFloat = view.width * 0.587
        let TextFieldHeight:CGFloat = 37
        let TextFieldLeftMargin:CGFloat = (view.width - TextFieldWidth) * 0.5
        let PlaceholderColor:UIColor = UIColor(hex: "0xC2C2C2")
        let InputColor:UIColor = UIColor.white
        
        _telTextField.frame = CGRectMake(TextFieldLeftMargin, 80, TextFieldWidth, TextFieldHeight)
        //_telTextField.textInset = UIEdgeInsetsMake(0, 19, 0, 0)
        _telTextField.font = UIFont.systemFont(ofSize:13)
        _telTextField.textAlignment = .center
        _telTextField.keyboardType = .phonePad
        _telTextField.clearButtonMode = .whileEditing
        //_telTextField.delegate = self
        _telTextField.text = "18588210810"
        _telTextField.backgroundColor = UIColor(white: 0.97, alpha: 1)
        _telTextField.attributedPlaceholder = NSMutableAttributedString(string: "输入Ta的手机号码", attributes: [NSForegroundColorAttributeName:PlaceholderColor])
        _telTextField.textColor = InputColor
        view.addSubview(_telTextField)
        
        let invitationBtnSize = CGSizeMake(100, 40)
        let invitationBtn:UIButton = UIButton(type:.system)
        invitationBtn.setTitle("邀请", for: .normal)
        invitationBtn.backgroundColor = UIColor.lightGray
        invitationBtn.frame = CGRectMake((view.width - invitationBtnSize.width) * 0.5, _telTextField.bottom + 30, invitationBtnSize.width, invitationBtnSize.height)
        invitationBtn.addTarget(self, action: #selector(invitationBtnTapped), for: .touchUpInside)
        view.addSubview(invitationBtn)
        
        _shareView.frame = CGRectMake(0, invitationBtn.bottom, view.width, 0)
        view.addSubview(_shareView)
        let invited = SimpleCookie.shared().getObject("Invited") as? Bool ?? false
        if !invited
        {
            _shareView.isHidden = true
        }
        
        let sloganLabel:UILabel = UILabel()
        sloganLabel.font = SWAppearanceManager.themeStyle.fontOfSize(15)
        sloganLabel.textColor = UIColor.darkGray
        sloganLabel.text = "发送下载链接给对方"
        sloganLabel.textAlignment = .center
        sloganLabel.frame = CGRectMake(0, 30, view.width, 13)
        _shareView.addSubview(sloganLabel)
        
        
        let buttonViewWidth:CGFloat = view.width - 40
        let buttonViewHeight:CGFloat = 80
        let buttonView:UIView = UIView()
        buttonView.frame = CGRectMake(20, sloganLabel.bottom + 30, buttonViewWidth, buttonViewHeight)
        _shareView.addSubview(buttonView)
        _shareView.height = buttonView.bottom
        
        let wechatBtn:UIButton = UIButton(type:.system)
        wechatBtn.backgroundColor = UIColor.lightGray
        wechatBtn.setTitle("微信邀请", for: .normal)
        wechatBtn.frame = CGRectMake(0, 0, buttonViewWidth, 35)
        wechatBtn.addTarget(self, action: #selector(wechatBtnTapped), for: .touchUpInside)
        buttonView.addSubview(wechatBtn)
        
        let QQBtn:UIButton = UIButton(type:.system)
        QQBtn.backgroundColor = UIColor.lightGray
        QQBtn.setTitle("QQ邀请", for: .normal)
        QQBtn.frame = CGRectMake(0, wechatBtn.bottom + 10, buttonViewWidth, 35)
        QQBtn.addTarget(self, action: #selector(QQBtnTapped), for: .touchUpInside)
        buttonView.addSubview(QQBtn)
    }
    
    private func invite(phone:String)
    {
        let req = SWFamilyInviteReq()
        req.phone = phone
        req.successCallback = {[weak self] response,cache in
            SimpleCookie.shared().setObject(true, forKey: "Invited")
            SVProgressHUD.showSuccess(withStatus:"邀请短信已发送")
            self?._shareView.isHidden = false
        }
        
        req.failCallback = {[weak self] error in
            if error.code == 3026{
                SVProgressHUD.showError(withStatus:"不能邀请自己")
            }else{
                SVProgressHUD.showError(withStatus:"邀请失败，请重试")
            }
        }
        load(req)
    }
    
    @objc private func invitationBtnTapped()
    {
        guard let phone = _telTextField.text , phone != "" else
        {
            SVProgressHUD.showError(withStatus:"输入Ta的手机号码")
            return
        }
        
        _telTextField.resignFirstResponder()
        
        invite(phone: phone)
    }
    
    @objc private func wechatBtnTapped()
    {
        //let inviteCode:String = SWAccountManager.sharedManager().account?.familyID ?? ""
        //let inviteText:String = "下载“咿呀相机”输入邀请码:\(inviteCode)就能看见宝宝的照片"
        //WeixinHelper.shared().sendText(inviteText, scene: Int(WXSceneSession.rawValue))
        
        let inviteText:String = "下载“咿呀相机”，用小宝的照片，共建爱的小屋"
        //WeixinHelper.shared().sendLink(SW.Config.Tencent_Store_URL, scene: Int(WXSceneSession.rawValue), title: "咿呀相机", desc: inviteText)
    }
    
    @objc private func QQBtnTapped()
    {
        //let inviteCode:String = SWAccountManager.sharedManager().account?.familyID ?? ""
        //let inviteText:String = "下载“咿呀相机”输入邀请码:\(inviteCode)就能看见宝宝的照片"
        //QQSDKHelper.shared().shareText(inviteText)
        
        let inviteText:String = "下载“咿呀相机”，用小宝的照片，共建爱的小屋"
        //QQSDKHelper.shared().shareLink(URL(string:SW.Config.Tencent_Store_URL)!, scene: QQSDK.ShareScene.Session, title: "咿呀相机", description: inviteText, previewImageURL: nil)
    }
    
    @objc private func viewTapped()
    {
        view.endEditing(true)
    }
    
    
}
